package com.sltx.controller;

import com.sltx.controller.base.BaseController;
import com.sltx.service.ImageService;
import com.sltx.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/znbg")
public class ZnbgController extends BaseController {

    @Autowired
    private ImageService imageService;


    /**
     * 获取智能报告信息，根据传入的file_type选择对应报告内容
     * @param request
     * @return
     */
    @RequestMapping("/getZnbg")
    @ResponseBody
    public Object getlyjj(HttpServletRequest request) {
        PageData pd = this.getPageData();
        int pageSize = Integer.parseInt(pd.get("pageSize").toString());
        int pageNo = Integer.parseInt(pd.get("pageNo").toString());

        Integer startPage = ((pageSize*(pageNo-1)));
        Integer endPage = startPage+pageSize;

        pd.put("startPage",startPage.toString());
        pd.put("endPage",endPage.toString());

        Map<String,Object> result = new HashMap<String, Object>();

        List<Object> getlyjj = imageService.getlyjj(pd);
        int i = imageService.getlyjjNum(pd);
        result.put("num", i);
        result.put("list", getlyjj);

        return result;
    }

    @RequestMapping("/insertZnbg")
    @ResponseBody
    public String insertZnbg( HttpServletRequest request) {
        PageData pd = this.getPageData();
        String pdf = pd.get("pdf").toString();
        pd.put("pdf_url",pdf);

        int num = Integer.parseInt(pd.get("num").toString());
        if (1 == num){
            int i = imageService.updateZnbg(pd);
            if (i == 0){
                return "编辑失败";
            }
            return "编辑成功";
        }else if (0 == num) {
            int i = imageService.insertZnbg(pd);
            if (i == 0){
                return "上传失败";
            }
            return "上传成功";
        }
        return "文件上传失败";
    }

    @RequestMapping("/insertFavorite")
    @ResponseBody
    public String insertFavorite( HttpServletRequest request) {
        PageData pd = this.getPageData();

        int i = imageService.insertFavorite(pd);
        if(i == 0 ){
            return "收藏失败";
        }
        return "收藏成功";
    }


    @RequestMapping("/deleteFavorite")
    @ResponseBody
    public String deleteFavorite( HttpServletRequest request) {
        PageData pd = this.getPageData();

        int i = imageService.deleteFavorite(pd);
        if(i == 0){
            return "取消收藏失败";
        }
        return "取消收藏成功";
    }

    /**
     * 获取所有文件列表
     * @param request
     * @return
     */
    @RequestMapping("/getList")
    @ResponseBody
    public Object getList(HttpServletRequest request) {
        PageData pd = this.getPageData();

        int pageSize = Integer.parseInt(pd.get("pageSize").toString());
        int pageNo = Integer.parseInt(pd.get("pageNo").toString());

        Integer startPage = ((pageSize*(pageNo-1)));
        Integer endPage = startPage+pageSize;

        pd.put("startPage",startPage.toString());
        pd.put("endPage",endPage.toString());

        Map<String,Object> result = new HashMap<String, Object>();

        result.put("num", imageService.getListNum(pd));
        result.put("date",imageService.getList(pd));

        return result;
    }


    /**
     * 获取本部门上传文件列表
     * @param request
     * @return
     */
    @RequestMapping("/getDeptList")
    @ResponseBody
    public Object getDeptList(HttpServletRequest request) {
        PageData pd = this.getPageData();
        int pageSize = Integer.parseInt(pd.get("pageSize").toString());
        int pageNo = Integer.parseInt(pd.get("pageNo").toString());

        Integer startPage = ((pageSize*(pageNo-1)));
        Integer endPage = startPage+pageSize;

        pd.put("startPage",startPage.toString());
        pd.put("endPage",endPage.toString());

        Map<String,Object> result = new HashMap<String, Object>();
        result.put("num",imageService.getDeptListNum(pd));
        result.put("data",imageService.getDeptList(pd));
        return result;
    }


    /**
     * 获取我收藏的报告
     * @param request
     * @return
     */
    @RequestMapping("/getFavorite")
    @ResponseBody
    public Object getFavorite(HttpServletRequest request) {
        PageData pd = this.getPageData();
        int pageSize = Integer.parseInt(pd.get("pageSize").toString());
        int pageNo = Integer.parseInt(pd.get("pageNo").toString());

        Integer startPage = ((pageSize*(pageNo-1)));
        Integer endPage = startPage+pageSize;

        pd.put("startPage",startPage.toString());
        pd.put("endPage",endPage.toString());

        Map<String,Object> result = new HashMap<String, Object>();
        result.put("num", imageService.getFavoriteNum(pd));
        result.put("data", imageService.getFavorite(pd));

        return result;
    }
}
